<?php

namespace app\modules\home\controllers;

use Yii;
use app\controllers\ApiBaseController;
use app\modules\home\actions as actions;

class UserController extends ApiBaseController {
    
    public function init(){
        
    }

    /**
     * 注册用户
     * @param  array $arrInput
     * @return
     */
    public function actionRegister() {
        $data = Yii::$app->request->get();
        if(empty($data)) {
            $data = Yii::$app->request->post();
        }
        $action = new actions\User();
        $ret = $action->Register($data);
        return $ret;
    }
    
    /**
     * 登录用户
     * @param  array $arrInput
     * @return
     */
    public function actionLogin() {
        $data = Yii::$app->request->get();
        if(empty($data)) {
            $data = Yii::$app->request->post();
        }
        $action = new actions\User();
        $ret = $action->Login($data);
        return $ret;
    }
    
    //特殊接口
    public function actionLogind() {
//         $cookie = Yii::$app->request->cookies;
//         return $cookie;
        
        $data = Yii::$app->request->get();
        if(empty($data)) {
            $data = Yii::$app->request->post();
        }
        $uid = $data['uid'];
        if(!$uid) {
            formatJson(10001, '缺少uid');
        }
        
        //登录成功，设置
        yii::$app->session['uid'] = $uid;
        
//         $cookie = new \yii\web\Cookie();
//         $cookie->name = 'uid';
//         $cookie->value = $uid;
//         $cookie->expire = time() + 5; //6小时过期时间
//         $cookie->httpOnly = false;
        
        $cookie = new \yii\web\Cookie();
        $cookie->name = 'uid';
        $cookie->value = $uid;
        $cookie->expire = time() + 6*60*60; //6小时过期时间
        $cookie->httpOnly = false;
        
        Yii::$app->response->cookies->add($cookie);
        Yii::$app->response->send();
        
//         Yii::$app->response->cookies->add($cookie);
//         Yii::$app->response->send();
        
//         $action = new actions\User();
//         $ret = $action->Login($data);
        return $uid;
    }
    
    //退出
    public function actionLogout() {
        yii::$app->session['uid'] = '';
        //清除 cookie
        Yii::$app->response->cookies->remove('uid');
        Yii::$app->response->send();
        return true;
    }
    
    public function actionIslogin() {
        $uid = yii::$app->session['uid'];
        return $uid ? true : false;
    }
}